package yyee.llee.mmee.weather.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import yyee.llee.mmee.base_api_net.base_api_bean.HourlyFcst
import yyee.llee.mmee.common.databinding.ItemForecase24hBinding

class Forecast24hAdapter(val context: Context) :
    RecyclerView.Adapter<Forecast24hAdapter.Forecast24hViewHolder>() {

    private val mList: MutableList<HourlyFcst> = mutableListOf()

    class Forecast24hViewHolder(val binding: ItemForecase24hBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Forecast24hViewHolder {
        return Forecast24hViewHolder(
            ItemForecase24hBinding.inflate(
                LayoutInflater.from(context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: Forecast24hViewHolder, position: Int) {
        val date = mList[position].data_time.substring(11, 13)
        if (position == 0) {
            holder.binding.tvHour.text = "现在"
        } else {
            holder.binding.tvHour.text = date + "时"
        }
        holder.binding.ivHourStatus.setImageResourceName(mList[position].code)
        holder.binding.tvTemp.text = mList[position].temp_fc

    }

    override fun getItemCount(): Int {
        return if (mList.size == 0) 0 else mList.size
    }

    fun setData(hourlyFcsts: List<HourlyFcst>) {
        mList.clear()
        mList.addAll(hourlyFcsts)
        notifyDataSetChanged()
    }
}